//
//  UIButton+Extension.swift
//  ZZY微博
//
//  Created by 庄壮勇 on 2018/1/11.
//  Copyright © 2018年 Personal. All rights reserved.
//

import UIKit

extension UIButton {
    
    /// 遍历构造函数
    ///
    /// - parameter imageName:     图像名称
    /// - parameter backImageName: 背景图像名称
    ///
    /// - returns: UIButton
    convenience init(imageName: String, backImageName: String?) {
        self.init()
        
        setImage(UIImage(named: imageName), for: .normal)
        setImage(UIImage(named: imageName + "_highlighted"), for: .highlighted)
        
        if let backImgName = backImageName {
            setBackgroundImage(UIImage(named: backImgName), for: .normal)
            setBackgroundImage(UIImage(named: backImgName + "_highlighted"), for: .highlighted)
        }
        
        sizeToFit()
    }
    
    /// 遍历构造函数
    ///
    /// - parameter title:         title
    /// - parameter fontSize:      fontSize
    /// - parameter color:         color
    /// - parameter backImageName: backImageName
    ///
    /// - returns: UIButton
    convenience init(title: String, fontSize: CGFloat, color: UIColor, heighlightedColor: UIColor , backImageName: String? = nil) {
        
        self.init()
        
        setTitle(title, for: .normal)
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        setTitleColor(color, for: .normal)
        setTitleColor(heighlightedColor, for: .highlighted)
        if let backImgName = backImageName {
            setBackgroundImage(UIImage(named: backImgName), for: .normal)
        }
    }
}

